import * as actionTypes from "./action-types";
const initialState = {
    articles: [],
    page: 1,
    hasMore:true,
    categories: [],
    tags: []
}

export default (state = initialState, action) => {
    switch (action.type) {
        case actionTypes.STORE_PAGE:
            return Object.assign({}, state, {
                page: action.page
            })
        case actionTypes.STORE_HASMORE:
            return Object.assign({}, state, {
                hasMore: action.hasMore
            })
        case actionTypes.STORE_ARTICLES:
            let articles = []
            if(state.page==1){
                articles = action.articles;
            }else{
                articles = [...state.articles, ...action.articles];
            }
            return Object.assign({}, state, {
                articles
            })
        case actionTypes.SAVE_CATEGORIES:
            return Object.assign({}, state, {
                categories:action.categories
            })
        case actionTypes.SAVE_TAGS:
            return Object.assign({}, state, {
                tags:action.tags
            })
        case actionTypes.UPDATE_PV: //更新pv
            return Object.assign({}, state, {
                articles:state.articles.map(item => {
                    if (item._id === action.id) {
                        item.pv_count += 1
                    }
                    return item;
                })
            })
        case actionTypes.UPDATE_CMTNUM: //更新评论数量
            return Object.assign({}, state, {
                articles:state.articles.map(item => {
                    if (item._id === action.id) {
                        item.comment_count += 1
                    }
                    return item;
                })
            })
        default:
            return state
    }
}

